﻿Imports System.Activities
Imports System.Management
Imports System.Activities.Presentation.Metadata
Imports System.Drawing
Imports System.ComponentModel
Imports System.Activities.Presentation

''' <summary>サービスの実行状態を設定する Set service status</summary>
''' <remarks>Need Administrator 
''' http://msdn.microsoft.com/en-us/library/aa826699(VS.85).aspx</remarks>
Public Class WMISYSSetServiceStatus
    Inherits WMIExecuteActivityBase

    Public Property Status As InArgument(Of Integer)
    Public Property ServiceName As InArgument(Of String)

    Private _methodName As String = ""

    Public Sub New()
        Me.DisplayName = My.Resources.SetServiceStatusDisplayName
    End Sub

    Protected Overrides Function BeginExecute(ByVal context As AsyncCodeActivityContext, ByVal callback As AsyncCallback, ByVal state As Object) As IAsyncResult
        MyBase._target = "Win32_Service"
        MyBase._where = ""
        MyBase._service = "cimv2"

        MyBase._where = "Name='" + context.GetValue(Me.ServiceName) + "' "

        Select Case context.GetValue(Me.Status)
            Case 1 : _methodName = "StartService" 'Start
            Case 2 : _methodName = "StopService" 'Stop
            Case 3 : _methodName = "PauseService" 'Pause
            Case 4 : _methodName = "ResumeService" 'Resume
        End Select

        Return MyBase.BeginExecute(context, callback, state)
    End Function

    Protected Overrides Function ExecuteWMI(ByVal target As String, ByVal whereCondition As String, ByVal wmScope As ManagementScope) As List(Of Hashtable)
        Dim result As New List(Of Hashtable)
        Try
            wmScope = CreateScope(wmScope)

            Dim wmQuery As New ObjectQuery("SELECT * FROM " + target + " WHERE " + whereCondition)
            Dim wmResult As New ManagementObjectSearcher(wmScope, wmQuery)
            For Each res As ManagementObject In wmResult.Get
                Dim outParams = res.InvokeMethod(_methodName, Nothing, Nothing)
                If outParams IsNot Nothing Then
                    Dim resRec As New Hashtable
                    For Each resCol In outParams.Properties
                        If resCol.Value Is Nothing Then
                            resRec.Add(resCol.Name, Nothing)
                        Else
                            resRec.Add(resCol.Name, resCol.Value.ToString)
                        End If
                    Next
                    result.Add(resRec)
                End If
            Next
        Catch ex As Exception
            Console.WriteLine("Exception : " + ex.Message)
            result.Clear()
        End Try
        Return result
    End Function

    Protected Overrides Sub CacheMetadata(metadata As CodeActivityMetadata)
        MyBase.CacheMetadata(metadata)

        Dim build As New AttributeTableBuilder
        'WMISYSSetServiceStatus Activity
        build.AddCustomAttributes(Me.GetType,
                                      New DesignerAttribute(GetType(WMIDefaultActivityDesigner), GetType(ActivityDesigner)),
                                      New ToolboxBitmapAttribute(Me.GetType, "WmiDefault.png"))

        build.AddCustomAttributes(GetType(WMISYSSetServiceStatus), "Status",
                                  New RequiredArgumentAttribute(),
                      New CategoryAttribute(My.Resources.CategoryDetail),
                      New DisplayNameAttribute(My.Resources.StatusDisplay),
                      New DescriptionAttribute(My.Resources.StatusDescription))
        build.AddCustomAttributes(GetType(WMISYSSetServiceStatus), "ServiceName",
                                  New RequiredArgumentAttribute(),
                              New CategoryAttribute(My.Resources.CategoryDetail),
                              New DisplayNameAttribute(My.Resources.ServiceNameDisplay),
                              New DescriptionAttribute(My.Resources.ServiceNameDescription))

        MetadataStore.AddAttributeTable(build.CreateTable)
    End Sub

End Class
